package com.hontech.bvapp2.ui.parts.door

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.FrameLayout
import com.hontech.bvapp2.R
import com.hontech.bvapp2.device.cp.Door
import com.hontech.bvapp2.ui.view.LabelState
import com.hontech.bvapp2.ui.view.LabelTextView
import com.hontech.bvapp2.utils.registerEvent
import com.hontech.bvapp2.utils.unregister
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class DoorStatusParts (context: Context, attrs: AttributeSet) : FrameLayout(context, attrs) {

    private val lsDoorClose: LabelState
    private val lsDoor2Close: LabelState
    private val lsPickClose: LabelState
    private val lsPickOpen: LabelState
    private val lsPick2: LabelState
    private val lsCupChecked: LabelState
    private val ltAdc: LabelTextView

    init {
        val view = LayoutInflater.from(context).inflate(R.layout.parts_door_status, this)
        lsDoorClose = view.findViewById(R.id.id_door_status_ls_door_close)
        lsDoor2Close = view.findViewById(R.id.id_door_status_ls_door2_close)
        lsPickClose = view.findViewById(R.id.id_door_status_ls_pick_close)
        lsPickOpen = view.findViewById(R.id.id_door_status_ls_pick_open)
        lsPick2 = view.findViewById(R.id.id_door_status_ls_pick2)
        lsCupChecked = view.findViewById(R.id.id_door_status_ls_cup_checked)
        ltAdc = view.findViewById(R.id.id_door_status_lt_adc)
    }

    override fun onWindowVisibilityChanged(visibility: Int) {
        super.onWindowVisibilityChanged(visibility)
        if (visibility == VISIBLE) {
            registerEvent(this)
        } else {
            unregister(this)
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN_ORDERED)
    fun onStatus(status: Door.Status) {
        lsDoorClose.set(status.doorClose())
        lsDoor2Close.set(status.door2Close())
        lsPickClose.set(status.pickClose())
        lsPickOpen.set(status.pickOpen())
        lsPick2.set(status.pick2())
        lsCupChecked.set(status.cupChecked())
        ltAdc.set(status.analog)
    }
}

